package com.linkage.jz.bankruptcy.activity.Vote;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.base.BaseCreateActivity;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteDetail;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteResult;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.widget.CommonPopupMenu;
import com.linkage.jz.bankruptcy.widget.CommonPromptDialog;
import com.linkage.seapeak.activity.user.ImageShowActivity;
import com.linkage.seapeak.utils.FileUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author AFAP
 * 表决结果文件发布
 */
public class ProVoteResultFileActivity extends BaseCreateActivity implements View.OnClickListener {
    public static final String KEY_RESULT = "KEY_RESULT";
    public static final String KEY_VOTE = "vote";

    private ProVoteDetail mProVoteDetail;
    private ProVoteResult mProVoteResult;
    private String mProjectId;

    private ImageView img_vote_result;

    @Override
    public void initData() {
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pro_vote_result_file);
        super.onCreate(savedInstanceState);
        titlebar_title.setText("表决结果文件");

        mProjectId = getIntent().getStringExtra(KEY_PROJECTID);
        if (getIntent().getSerializableExtra(KEY_RESULT) != null) {
            mProVoteResult = (ProVoteResult) getIntent().getSerializableExtra(KEY_RESULT);
            mProVoteDetail = (ProVoteDetail) getIntent().getSerializableExtra(KEY_VOTE);
            initViews();
        } else {
            mProVoteDetail = (ProVoteDetail) getIntent().getSerializableExtra(KEY_VOTE);
            getResult();
        }
    }


    CommonPopupMenu popupMenu;

    public void initViews() {
        titlebar_menu.setVisibility(View.VISIBLE);
        img_vote_result = findViewById(R.id.img_vote_result);
        Glide.with(this).load(mProVoteResult.getFileUrl())
                .into(img_vote_result);

        List<String> menus = new ArrayList<>();
        menus.add("下载");
        menus.add("重新生成");
        if (mProVoteDetail.getResultStatus() != 1) { // 未发布可重新生成
            findViewById(R.id.tips).setVisibility(View.VISIBLE);
            findViewById(R.id.btn_vote_result_publish).setVisibility(View.VISIBLE);
        }

        if (mProVoteDetail.getResultStatus() == 1) {
            findViewById(R.id.btn_vote_result_publish).setVisibility(View.GONE);
        } else {
            findViewById(R.id.btn_vote_result_publish).setVisibility(View.VISIBLE);
        }
        popupMenu = new CommonPopupMenu(this, menus);
        titlebar_menu.setOnClickListener(v -> {
            popupMenu.showLocation(R.id.titlebar_menu);// 设置弹出菜单弹出的位置
            popupMenu.setOnItemClickListener((index, str) -> {
                popupMenu.dismiss();
                if (TextUtils.equals(str, "重新生成")) {
                    Intent intent = new Intent(ProVoteResultFileActivity.this, ProVoteCreateResultActivity.class);
                    intent.putExtra(ProVoteCreateResultActivity.KEY_VOTE, mProVoteDetail);
                    intent.putExtra(ProVoteCreateResultActivity.KEY_RECREATE, true);
                    intent.putExtra(ProVoteResultFileActivity.KEY_RESULT, mProVoteResult);
                    startActivity(intent);
                    finish();
                } else if (TextUtils.equals(str, "下载")) {
                    FileUtils.saveNetImageToGallery(ProVoteResultFileActivity.this, mProVoteResult.getFileUrl());
                }
            });
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_vote_result_publish:
                saveData();
                break;
        }
    }

    private void getResult() {
        showLoading("加载中...");
        Network.getAPIService()
                .getVoteResult_Manager(getUserToken(), mProVoteDetail.getId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        mProVoteResult = ProVoteResult.parse(json.getAsJsonObject("data"));
                        initViews();
                    }

                    @Override
                    public void onCompleted() {
                        hideLoading();
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        super.onError(e);
                    }
                });
    }


    public void saveData() {
        showLoading("提交中...");

        Network.getAPIService()
                .publishVoteResult_Manager(getUserToken(), mProVoteDetail.getId(), mProjectId,
                        mProVoteResult.getResultId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }

                        CommonPromptDialog dialog = new CommonPromptDialog(ProVoteResultFileActivity.this,
                                CommonPromptDialog.TYPE_TIPS);
                        dialog.setContent("已发布");
                        dialog.setCancelable(false);
                        dialog.setmCloseListener(v -> {
                            dialog.dismiss();
                            RefreshEvent event = new RefreshEvent();
                            event.setType(RefreshEvent.TYPE_VOTE);
                            EventBus.getDefault().post(event);
                            finish();
                        });
                        dialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        hideLoading();
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        super.onError(e);
                    }
                });
    }

}
